//
//  NetEaseDataService.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/30.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NetEaseDataService.h"

@implementation NetEaseDataService

#pragma mark - 通用型的网络接口
+ (AFHTTPRequestOperation*)netEaseRequestUrl:(NSString *)urlStr httpMethod:(NSString *)method params:(NSMutableDictionary *)params datas:(NSMutableDictionary *)datas block:(BlockType)block {
    //01 构建url
    //02 构建参数
    
    //03 构建manager
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    //04 发送请求
    AFHTTPRequestOperation* operation;
    if ([method isEqualToString:@"GET"]) {
        operation = [manager GET:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"get请求成功");
            block(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"get请求失败  error%@", error);
            block(error);
        }];
        //05 监听下载进度
        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            NSLog(@"共需下载:%lld, 已经下载:%lld", totalBytesExpectedToRead, totalBytesRead);
        }];
        
    } else if ([method isEqualToString:@"POST"]) {
        if (datas) {        //如果发微博带图片
            operation = [manager POST:urlStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                for (NSString* name in datas) {
                    NSData* data = [datas objectForKey:name];
                    [formData appendPartWithFileData:data name:name fileName:@"1.png" mimeType:@"image/jpeg"];
                }
            } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"带数据的POST请求成功");
                block(responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"带数据的POST请求失败  error:%@", error);
                block(error);
            }];
        } else {            //如果微博不带图片
            operation = [manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"不带数据的POST请求成功");
                block(responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"不带数据的POST请求失败  error:%@", error);
                block(error);
            }];
        }
        
        //05 监听上传进度
        [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
            NSLog(@"共需上传:%lld, 已经上传:%lld", totalBytesExpectedToWrite, totalBytesWritten);
        }];
    }
    
    return operation;
}

#pragma mark - 新闻页面中：头条、NBA、娱乐、时尚、汽车、房产、段子 以及24小时要闻 的数据获取
//新闻页面，头条 的数据获取
+ (AFHTTPRequestOperation*)newsHeadNewsDataRequestFrom:(NSInteger)from
                                                  size:(NSInteger)size
                                                 block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline/T1348647853363/%li-%li.html", from, size];

    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//新闻页面，NBA 的数据获取
+ (AFHTTPRequestOperation*)newsNBANewsDataRequestFrom:(NSInteger)from
                                                 size:(NSInteger)size
                                                block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1348649145984/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//新闻页面，娱乐 的数据获取
+ (AFHTTPRequestOperation*)newsEntertainmentNewsDataRequestFrom:(NSInteger)from
                                                           size:(NSInteger)size
                                                          block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1348648517839/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//新闻页面，时尚 的数据获取
+ (AFHTTPRequestOperation*)newsFashionNewsDataRequestFrom:(NSInteger)from
                                                     size:(NSInteger)size
                                                    block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1348650593803/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//新闻页面，汽车 的数据获取
+ (AFHTTPRequestOperation*)newsCarNewsDataRequestFrom:(NSInteger)from
                                                 size:(NSInteger)size
                                                block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/auto/list/5YWo5Zu9/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//新闻页面，房产 的数据获取
+ (AFHTTPRequestOperation*)newsHouseNewsDataRequestFrom:(NSInteger)from
                                                   size:(NSInteger)size
                                                  block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/house/5YWo5Zu9/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//新闻页面，段子 的数据获取
+ (AFHTTPRequestOperation*)newsDuanziNewsDataRequestSize:(NSInteger)size
                                                   block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/recommend/getChanRecomNews?channel=duanzi&passport=&devId=EB50967A-A384-4446-8701-029950341DF9&size=%li", size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//24小时要闻 的数据获取
+ (AFHTTPRequestOperation*)news24HoursDataRequestFrom:(NSInteger)from
                                                 size:(NSInteger)size
                                                block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1429173683626/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}

#pragma mark - 图集数据请求
//新闻页 图集
+ (AFHTTPRequestOperation*)photoShowDataRequestSkipId:(NSString*)skipId
                                                       type:(NSString *)type
                                                      block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@/%@.json", type, skipId];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}

#pragma mark - 新闻细节页数据请求
//新闻页 新闻详情页
+ (AFHTTPRequestOperation*)newsDetailDataRequestNewsId:(NSString*)newsId
                                                 block:(BlockType)block {
    
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", newsId];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}

#pragma mark - 跟帖详情 数据请求.
+ (AFHTTPRequestOperation*)replyDetailRequestReplyId:(NSString*)replyId
                                           replyType:(NSString*)replyType       //hot、normal
                                               block:(BlockType)block {
    if (![replyType isEqualToString:@"hot"] && ![replyType isEqualToString:@"normal"]) return nil;
    
    NSString* urlStr = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/%@/comment_bbs/%@/desc/0/10/10/2/2", replyType, replyId];
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}

#pragma mark - 阅读页 数据请求
+ (AFHTTPRequestOperation*)readerRequestSize:(NSInteger)size
                                       block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=EB50967A-A384-4446-8701-029950341DF9&size=%li&version=5.3.4&spever=false&net=wifi&lat=0.000000&lon=0.000000&from=yuedu", size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];

}

#pragma mark - 视听页 数据请求
//视频
+ (AFHTTPRequestOperation*)mediaVideoRequestFrom:(NSInteger)from
                                            size:(NSInteger)size
                                           block:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/home/%li-%li.html", from, size];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}
//电台
+ (AFHTTPRequestOperation*)mediaRadioRequestBlock:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/topicset/ios/radio/index.html"];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}

#pragma mark - 发现页 数据请求
+ (AFHTTPRequestOperation*)foundDataRequestBlock:(BlockType)block {
    NSString* urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/topicset/uc/api/discovery/indexV52"];
    
    return [self netEaseRequestUrl:urlStr httpMethod:@"GET" params:nil datas:nil block:block];
}


@end
